<?php 
//评论接口

	require '../../frame/base.php';
	require('../../tool/UploadImage.class.php');

	if(!isset($_REQUEST['data'])){
		echo json_encode(array('code'=>100,'msg'=>'信息异常'));
		exit;

	}
	$data = json_decode($_REQUEST['data'],true);
	if(!$data){
		echo json_encode(array('code'=>100,'msg'=>'信息异常'));
		exit;
	}
	if(!isset($_REQUEST['userid']) || !is_numeric($_REQUEST['userid'])){
		echo json_encode(array('code'=>100,'msg'=>'信息异常'));
		exit;

	}
	if(!isset($_REQUEST['shopid']) || !is_numeric($_REQUEST['shopid'])){
		echo json_encode(array('code'=>100,'msg'=>'信息异常'));
		exit;
		
	}
	if(!isset($_REQUEST['ordersid']) || !is_numeric($_REQUEST['ordersid'])){
		echo json_encode(array('code'=>100,'msg'=>'信息异常'));
		exit;
	}
	$wuliu_service = 5;
	$taidu_service = 5;
	$shopid = $_REQUEST['shopid'];
	$ordersid = $_REQUEST['ordersid'];
	$userid = $_REQUEST['userid'];

	if(isset($_REQUEST['wuliu_service']) || is_numeric($_REQUEST['wuliu_service'])){
		$wuliu_service = $_REQUEST['wuliu_service'];
	}

	if(isset($_REQUEST['taidu_service']) || is_numeric($_REQUEST['taidu_service'])){
		$taidu_service = $_REQUEST['taidu_service'];
	}

	$Db->startTrans();
	foreach ($data as $key => $value) {
		$comments = $value['comments'];
		$is_nobody = $value['is_nobody'];
		$describe_comments = $value['describe_comments'];
		$goodsid = $value['goodsid'];
		$srcimages = "";
		$subimages = "";
		
		if (isset($_FILES['comments_pic'.$key]) && $_FILES['comments_pic'.$key]['name'][0] != '') {
			$upload = new Upload('comments_pic'.$key);
			$date = date('Y-m-d');
			$sumbimage = '../../sumbimage/'.$date;
			$srcimage = '../../srcimage/'.$date;
			$upload->setFilePath($srcimage);
			$upload->setSmallFilePath($sumbimage);
			$re = $upload->upload();
			if($re){
				$path = $upload->getPath();
				$smallPath = $upload->getSmallimg();
				
				foreach ($path as $key => $value) {
					$path[$key] = 'srcimage/'.$date.'/'.$value;
					$smallPath[$key] = 'sumbimage/'.$date.'/'.$smallPath[$key];
				}
			
				$srcimages = implode(',',$path);
				$subimages = implode(',',$smallPath);
			}
		}

		$data1 = [
			'comments'         =>	$comments,
			'comments_pic'     =>	$srcimages,
			'sub_comments_pic' =>	$subimages,
			'is_nobody'        =>	$is_nobody,
			'describe_goods'   =>	$describe_comments,
			'wuliu_service'    =>	$wuliu_service,
			'taidu_service'    =>	$taidu_service,
			'comment_state'    =>	'1',
			'state'            =>	'5',
		];

		//店铺评分
		$_wuliu = $Db->table('goodsorders')->field("AVG(wuliu_service)")->where('shopid',$shopid)->find();
		$_wuliu  =round($_wuliu['AVG(wuliu_service)']);

		$_taidu = $Db->table('goodsorders')->field("AVG(taidu_service)")->where('shopid',$shopid)->find();
		$_taidu = round($_taidu['AVG(taidu_service)']);

		$data2 = [
			'wuliu_service'=>$_wuliu,
			'taidu_service'=>$_taidu,
		];

		$result = $Db->table('car')->where(array('ordersid'=>$ordersid,'goodsid'=>$goodsid,'userid'=>$userid))->update($data1);
		if(!$result){
			$Db->rollback();
			echo json_encode(array('code'=>100,'msg'=>'评论失败1'));
			exit;
			
		}
	}
	// $Db->fetchSql(true);
	$orders = $Db->table('goodsorders')->where(array('ordersid'=>$ordersid,'userid'=>$userid))->field('shou_huo_datetime, fan_juan_price')->find();


	$result = $Db->table('goodsorders')->where(array('ordersid'=>$ordersid,'userid'=>$userid))->update(array('state'=>5,'addtime'=>date("Y-m-d H:i:s"),'ping_lun_datetime' => date("Y-m-d H:i:s")));

	if(!$result){
		$Db->rollback();
		echo json_encode(array('code'=>100,'msg'=>'评论失败2'));
		exit;
	}

	//如果评论超过七天不返回积分返券
	$time = date("Y-m-d H:i:s",strtotime("+7 day",strtotime($orders['shou_huo_datetime'])));
	if(date('Y-m-d H:i:s') < $time) {
/*		$user = $Db->table('user')->where('userid = ' . $_POST['userid'])->field('return_volume')->find();
		$shop = $Db->table('shop')->where(array('shopid' => $_POST['shopid']))->field('balance')->find();
		$return_volume = $Db->table('user')->where('userid = ' . $_POST['userid'])->update(array('return_volume' => $user['return_volume']+$orders['fan_juan_price']));
		$balance = $Db->table('shop')->where('shopid = ' . $_POST['shopid'])->update(array('balance' => $shop['balance']-$orders['fan_juan_price']));
		$fan_juan_price = $Db->table('goodsorders')->where('ordersid = ' . $ordersid)->update(array('fan_juan_price' => 0));*/
		$fan_juan_price = $orders['fan_juan_price'];
	}
	//end
	
	$result2 = $Db->table('shop')->where('shopid',$shopid)->update($data2);
	$orderData = $Db->field('a.danhao,a.shopid,express_delivery.pinyin')->table('goodsorders')->alias('a')->join('express_delivery','express_delivery.id = a.kuaidi')->where('ordersid',$ordersid)->find();
	$orderData['fan_juan_price'] = $fan_juan_price;
	$Db->commit();
	echo json_encode(array('code'=>200,'msg'=>'评论成功','data'=>$orderData));

 ?>		
